//
//  AudioQuestionsVC.m
//  ATAInterview
//
//  Created by up72-17 on 2018/12/10.
//  Copyright © 2018 up72-17. All rights reserved.
//

#import "AudioQuestionsVC.h"

@interface AudioQuestionsVC ()

@end

@implementation AudioQuestionsVC

- (void)dealloc {
    NSLog(@"AudioQuestionsVC dealloc");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [MTool setViewRadius:70 view:circleView];
    
    [timeLabel setText:@"00:30"];
    
    NSString *title = @"";
    NSString *questionsTitle = @"";
    NSString *questionsType = @"";
    
    if ([GlobalData sharedGlobalData].isSimulation) {
        if ([GlobalData sharedGlobalData].questionIndex == 0) {
            title = [NSString stringWithFormat:@"模拟试题1/%ld", [GlobalData sharedGlobalData].totalQuestion];
            questionsTitle = @"问题一";
            
            NSDictionary *questionDic = [[GlobalData sharedGlobalData].questionInfo objectAtIndex:0];
            questionsType = [questionDic objectForKey:@"questionsType"];
            
        } else if ([GlobalData sharedGlobalData].questionIndex == 1) {
            title = [NSString stringWithFormat:@"模拟试题2/%ld", [GlobalData sharedGlobalData].totalQuestion];
            questionsTitle = @"问题二";
            
            NSDictionary *questionDic = [[GlobalData sharedGlobalData].questionInfo objectAtIndex:1];
            questionsType = [questionDic objectForKey:@"questionsType"];
        } else if ([GlobalData sharedGlobalData].questionIndex == 2) {
            title = [NSString stringWithFormat:@"模拟试题3/%ld", [GlobalData sharedGlobalData].totalQuestion];
            questionsTitle = @"问题三";
            
            NSDictionary *questionDic = [[GlobalData sharedGlobalData].questionInfo objectAtIndex:2];
            questionsType = [questionDic objectForKey:@"questionsType"];
        } else if ([GlobalData sharedGlobalData].questionIndex == 3) {
            title = [NSString stringWithFormat:@"模拟试题4/%ld", (long)[GlobalData sharedGlobalData].totalQuestion];
            questionsTitle = @"问题四";
            
            NSDictionary *questionDic = [[GlobalData sharedGlobalData].questionInfo objectAtIndex:3];
            questionsType = [questionDic objectForKey:@"questionsType"];
        }
    } else {
        if ([GlobalData sharedGlobalData].questionIndex == 0) {
            title = [NSString stringWithFormat:@"试题1/%ld", [GlobalData sharedGlobalData].totalQuestion];
            questionsTitle = @"问题一";
            
            NSDictionary *questionDic = [[GlobalData sharedGlobalData].questionInfo objectAtIndex:0];
            questionsType = [questionDic objectForKey:@"questionsType"];
            
        } else if ([GlobalData sharedGlobalData].questionIndex == 1) {
            title = [NSString stringWithFormat:@"试题2/%ld", [GlobalData sharedGlobalData].totalQuestion];
            questionsTitle = @"问题二";
            
            NSDictionary *questionDic = [[GlobalData sharedGlobalData].questionInfo objectAtIndex:1];
            questionsType = [questionDic objectForKey:@"questionsType"];
        } else if ([GlobalData sharedGlobalData].questionIndex == 2) {
            title = [NSString stringWithFormat:@"试题3/%ld", [GlobalData sharedGlobalData].totalQuestion];
            questionsTitle = @"问题三";
            
            NSDictionary *questionDic = [[GlobalData sharedGlobalData].questionInfo objectAtIndex:2];
            questionsType = [questionDic objectForKey:@"questionsType"];
        } else if ([GlobalData sharedGlobalData].questionIndex == 3) {
            title = [NSString stringWithFormat:@"试题4/%ld", [GlobalData sharedGlobalData].totalQuestion];
            questionsTitle = @"问题四";
            
            NSDictionary *questionDic = [[GlobalData sharedGlobalData].questionInfo objectAtIndex:3];
            questionsType = [questionDic objectForKey:@"questionsType"];
        }
    }
    
    if ([questionsType isEqualToString:@"0"]) {
        [audioPlayerHeight setConstant:0];
    } else if ([questionsType isEqualToString:@"1"]) {
        [self.player setCurrentTime:0.0f];
        [self.player play];
    }
    
    [titleLable setText:title];
    [questionsTitleLabel setText:questionsTitle];
    
    
    NSDictionary *questionDic = [[GlobalData sharedGlobalData].questionInfo objectAtIndex:[GlobalData sharedGlobalData].questionIndex];
    [questionsContentLabel setText:[questionDic objectForKey:@"content"]];
    
    
    myProgressView.transform = CGAffineTransformMakeScale(1.0, 4.0);
    
    [myProgressView setProgressTintColor:kColorWithRGB(0x00D985)];
    [myProgressView setTrackTintColor:kColorWithRGB(0xD1F8E9)];
    
    [MTool setViewRadius:6 view:myProgressView];
    
    timeIndex = 30;
    
    [myProgressView setProgress:0];
    
    myTime = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timeing) userInfo:nil repeats:YES];
    
    fiveTime = [NSTimer timerWithTimeInterval:0.1f target:self selector:@selector(startFiveProgress) userInfo:nil repeats:YES];
    
    
    NSMutableArray *images = [NSMutableArray array];
    
    UIImage *image_1 = [UIImage imageNamed:@"语音_1"];
    UIImage *image_2 = [UIImage imageNamed:@"语音_2"];
    UIImage *image_3 = [UIImage imageNamed:@"语音_3"];
    [images addObject:image_1];
    [images addObject:image_2];
    [images addObject:image_3];
    
    [audioAnimationImageView setAnimationImages:images];
    [audioAnimationImageView setAnimationDuration:1.0f];
    [audioAnimationImageView startAnimating];
    
    processView = [[SABCircleProcessView alloc] initWithFrame:fiveSecondsContentBackView.bounds];
    [fiveSecondsContentBackView addSubview:processView];
    processView.ovalShapeLayer.strokeEnd = 1;
    
}

- (IBAction)backButtonAction:(id)sender {
    [self releaseMe];
//    [self.navigationController popViewControllerAnimated:YES];
    NSArray *temp = self.navigationController.viewControllers;
    for (NSInteger i = 0; i < [temp count]; i++) {
        id tempvc = [temp objectAtIndex:i];
        if ([tempvc isKindOfClass:[MainVC class]]) {
            [self.navigationController popToViewController:tempvc animated:YES];
            return;
        }
    }
}


- (void)timeing {
    
    NSInteger time = (NSInteger)self.player.duration -  (NSInteger)self.player.currentTime;
    [audioTimeLabel setText:[NSString stringWithFormat:@"%ld''", (long)time]];
    
    if (timeIndex <= 0) {
        [myTime invalidate];
        [self startButtonAction:nil];
    }
    
    if (timeIndex == 5) {
        fiveTimeIndex = 50;
        [[NSRunLoop currentRunLoop] addTimer:fiveTime forMode:NSRunLoopCommonModes];
        [fiveSecondsView setHidden:NO];
        
    }
    [fiveSecondsLabel setText:[NSString stringWithFormat:@"%ld", (long)timeIndex]];
    
    [timeLabel setText:[MTool convertTime:timeIndex]];
    [myProgressView setProgress:(30.0f - (CGFloat)timeIndex) / 30.0f];
    
    
    timeIndex--;
    
//    NSLog(@"timeIndex = %ld", (long)timeIndex);
}

- (void)startFiveProgress {
    
    fiveTimeIndex--;
    
    if (fiveTimeIndex <= 0) {
        [fiveSecondsView setHidden:YES];
        [fiveTime invalidate];
    }
    
    processView.ovalShapeLayer.strokeEnd = ((CGFloat)fiveTimeIndex) / 50.0f;
    
}


- (IBAction)startButtonAction:(id)sender {
    [self releaseMe];
    
    AnswerVC *answer = [[AnswerVC alloc] init];
    [self.navigationController pushViewController:answer animated:YES];
}



- (AVAudioPlayer *)player {
    if (_player == nil) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"mp3"];
        _player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:path] error:nil];
        [_player setDelegate:self];
        [_player prepareToPlay];
    }
    return _player;
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
    [audioAnimationImageView stopAnimating];
}

- (void)releaseMe {
    if (self.player.playing) {
        [self.player stop];
    }
    if (myTime.isValid) {
        [myTime invalidate];
    }
    if (fiveTime.isValid) {
        [fiveTime invalidate];
    }
}

@end
